package com.sdsy.util;


import org.apache.commons.codec.digest.DigestUtils;

import java.math.BigDecimal;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * @author bootdo
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {

    public String autocomplete(String seq) {
        return String.format("%06d", seq);
    }

    public static String generateRandomStr(int len) {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < len; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();
    }

    /**
     * 生成订单号
     */
    public static String getTransOrderNo(String bizType) {
        try {
            String times = DateFormat.dateToString(new Date(), CommTypeUtils.DateFormat.DatetimesFormatMilliSecond);
            String orderNo = bizType + times + RandomValidateCode.createRandomNumber(6);
            return orderNo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 明细流水号
     */
    public static String getAcctradeNo() {
        try {
            String times = DateFormat.dateToString(new Date(), CommTypeUtils.DateFormat.DatetimesFormatMilliSecond);
            String orderNo = times + RandomValidateCode.createRandomNumber(6);
            return orderNo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    public static String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"};


    /**
     * 6位 100000
     * 4位 1000
     *
     * @param m
     * @return
     */
    public static String getRandom(int m) {
        int i = (int) ((Math.random() * 9 + 1) * m);
        return i + "";
    }


    //生成随机数字和字母,  
    public static String getStringRandom(int length) {

        String val = "";
        Random random = new Random();

        //参数length，表示生成几位随机数  
        for (int i = 0; i < length; i++) {

            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字  
            if ("char".equalsIgnoreCase(charOrNum)) {
                //输出是大写字母还是小写字母  
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (random.nextInt(26) + temp);
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }

    /**
     * 金额校验
     *
     * @throws Exception
     */
//	public static void checkBalance(String freeNum, String frozenNum, String encryptBalance) throws BusinessException, Exception {
//		String balance = CommTypeUtils.CURRENCY_DF.format(new BigDecimal(freeNum).add(new BigDecimal(frozenNum)));
//		if(!MD5Utils.encrypt(balance, SaltCode.ACCOUNT_SALT, SaltCode.ACCOUNT_HASH_ITERATIONS).equals(encryptBalance)) {
//			throw new BusinessException(ResultErrorInfo.ACCOUNT_BALANCE_ERROR);
//		}
//	}
   /* public static boolean checkBalance(String freeNum, String frozenNum, String encryptBalance) {
        StringBuffer sb = new StringBuffer().append(SaltCode.ACCOUNT_SALT).append(freeNum).append(frozenNum).append(SaltCode.ACCOUNT_HASH_ITERATIONS);
        if (!DigestUtils.md5Hex(sb.toString()).equals(encryptBalance)) {
            return false;
        } else {
            return true;
        }
    }*/


    /**
     * 金额加密
     */
//	public static String encryptBalance(String freeNum, String frozenNum) {
//		String balance = CommTypeUtils.CURRENCY_DF.format(new BigDecimal(freeNum).add(new BigDecimal(frozenNum)));
//		return MD5Utils.encrypt(balance, SaltCode.ACCOUNT_SALT, SaltCode.ACCOUNT_HASH_ITERATIONS);
//	}


    /*public static String encryptBalance(String freeNum, String frozenNum) {
        StringBuffer sb = new StringBuffer().append(SaltCode.ACCOUNT_SALT).append(freeNum).append(frozenNum).append(SaltCode.ACCOUNT_HASH_ITERATIONS);
        return DigestUtils.md5Hex(sb.toString());
    }*/


    /**
     * 账户总金额
     *
     * @param freeNum
     * @param frozenNum
     * @return
     */
   /* public static String total(String freeNum, String frozenNum) {
        return CommTypeUtils.CURRENCY_DF.format(new BigDecimal(freeNum).add(new BigDecimal(frozenNum)));
    }*/


    /**
     * BigDecimal 除法
     *
     * @param v1    被除数
     * @param v2    除数
     * @param scale 保留小数位
     * @return
     */
    public static BigDecimal div(String v1, String v2, int scale) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);

        //四舍五入
//		BigDecimal result =  b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP); 

        // 最后一位向上取整
        BigDecimal result = b1.divide(b2, scale, BigDecimal.ROUND_CEILING);

        return result;
    }


    public static String md5(String data) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(data.getBytes());
        StringBuffer buf = new StringBuffer();
        byte[] bits = md.digest();
        for (int i = 0; i < bits.length; i++) {
            int a = bits[i];
            if (a < 0) a += 256;
            if (a < 16) buf.append("0");
            buf.append(Integer.toHexString(a));
        }
        return buf.toString();
    }

    public static String sha1(String data) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("SHA1");
        md.update(data.getBytes());
        StringBuffer buf = new StringBuffer();
        byte[] bits = md.digest();
        for (int i = 0; i < bits.length; i++) {
            int a = bits[i];
            if (a < 0) a += 256;
            if (a < 16) buf.append("0");
            buf.append(Integer.toHexString(a));
        }
        return buf.toString();
    }


    public static void sortStringArray(String[] arrayToSort) {
        for (int i = 0; i < arrayToSort.length; i++) {
            System.out.print(arrayToSort[i] + ",");
        }
        // 调用数组的静态排序方法sort,且不区分大小写
        Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
        for (int i = 0; i < arrayToSort.length; i++) {
            System.out.print(arrayToSort[i] + ",");
        }
    }

}
